﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using CruxCRUDBuilderHelper.DTOs;

namespace CruxCRUDBuilderHelper.Utility
{
    public class ProjectSettingParser
    {
        public List<ProjectSettingInfo> GetProjectSettings(string projectName)
        {
            XDocument mappingConfig = new XDocument();
            mappingConfig = XDocument.Load(@"ProjectSetting.config");
            List<XElement> projects =
                mappingConfig.Element("configuration").Element("Projects").Elements("Project").ToList();
            XElement specificProject =
                projects.Where(a => a.Attribute("Name").Value.Equals(projectName)).SingleOrDefault();
            if (specificProject != null)
            {
                List<XElement> settings = specificProject.Descendants().ToList();

                return settings.Select(p => new ProjectSettingInfo

                                                {
                                                    Name = p.Attribute("Name").Value,
                                                    Value = p.Attribute("Value").Value
                                                }
                    ).ToList();
            }
            return new List<ProjectSettingInfo>();
        }

        public List<string> GetProjects()
        {
            XDocument mappingConfig = new XDocument();
            mappingConfig = XDocument.Load(@"ProjectSetting.config");
            List<XElement> projects =
                mappingConfig.Element("configuration").Element("Projects").Elements("Project").ToList();
            return projects.Select(a => a.Attribute("Name").Value).ToList();
        }
    }
}
